﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Btssio.Musique;
namespace dllTeamTard
{
   public static class LastFmApi
    {
        #region Champs
        private static string url_getEventbyArtist = "http://ws.audioscrobbler.com/2.0/?method=artist.getevents&artist=#artist#&api_key=62344f43eff87194ffce0a9fc833d8fb";

        private static string url_getEventbyVille = "http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=#ville#&api_key=62344f43eff87194ffce0a9fc833d8fb";


        #endregion

        #region proprietés

        /* public string urlgetEventByArtist
        {
            get { return _urlgetEventbyArtist; }
            set { _urlgetEventbyArtist = value; }
        }

        public string urlgetEventbyVille
        {
            get { return _urlgetEventbyVille; }
            set { _urlgetEventbyVille = value; }
        }
        */
        #endregion
        
       
        #region Méthodes



        public static List<Event> getEventsbyArtist (string nomArtist)
        {
            List<Event> lesEvents = new List<Event>();
            string urlgetEventByArtist = url_getEventbyArtist.Replace("#artist#",nomArtist);
            IEnumerable<XElement>xLesEvents = XDocument.Load(urlgetEventByArtist).Root.Element("events").Elements("event");
            int i=1;
            foreach (XElement xUnEvent in xLesEvents)
            {
                DateTime date = new DateTime();
                string ladate = xUnEvent.Element("startDate").Value;
                date = Convert.ToDateTime(ladate);
                //Création de l'Event
                Event unEvent = new Event(xUnEvent.Element("id").Value,
                    xUnEvent.Element("title").Value,
                    xUnEvent.Element("url").Value,
                    xUnEvent.Element("description").Value,
                    //Merci pour l'aide de Agueri
                    xUnEvent.Elements("image").ElementAt(2).Value,
                    date);

                lesEvents.Add(unEvent);
                i++;
            }
            return lesEvents;
        }
       //marche pas partie a refaire par remy
        public static List<Event> getEventsbyVille (string nomVille)
        {
         List<Event> lesEvents = new List<Event>();
        string urlEventbyVille = url_getEventbyVille.Replace("#ville#",nomVille);
        IEnumerable<XElement> xLesEvents = XDocument.Load(urlEventbyVille).Root.Element("data").Elements("event");
        int i=1;
            foreach (XElement xUnEvent in  xLesEvents)
            {
                Event unEvent = new Event (xUnEvent.Element("id").Value,
                                  xUnEvent.Element("titre").Value,
                                  xUnEvent.Element("description").Value,
                                  (int)xUnEvent.Element("nbparticipant")
                                  );
                lesEvents.Add(unEvent);
                i++;
            }
            return lesEvents;
        }
       
        #endregion
    }
}
